Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Error queries on subgraphs #936

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

qgatssdev
Copy link
Contributor

Description

This PR addresses an issue where the application fails to properly handle errors from The Graph API. Even though the API returns a 200 OK HTTP status code, it includes error information in the response body that wasn't being checked.

  • Updated the GraphResponse type to include error fields
  • Added a checkGraphQLErrors helper function to properly detect and handle GraphQL errors
  • Added an ensureJsonParsed helper function to handle both string and JSON responses consistently
  • Modified the subgraph query functions to check for errors before processing data
  • Improved error messages to make debugging easier

Related issue(s)

Closes #931
Fixes #931

Checklist

  • Manual testing passed.
  • Automated tests added, or N/A.
  • Documentation updated, or N/A.
  • Environment variables set in CI, or N/A.

@qgatssdev
Copy link
Contributor Author

@gndelia Please how can this be tested? I resolved this issue based on the screenshot you shared, I would love to know how to recreate and test.

@gndelia
Copy link
Contributor

gndelia commented Mar 3, 2025

@gndelia Please how can this be tested? I resolved this issue based on the screenshot you shared, I would love to know how to recreate and test.

Thanks for your contribution!

These scenarios are always hard to test. You can always replace the fetch call with a setTimeout + returning a hardcoded response. If you want to get more sophisticated, you can use libraries such as msw to mock responses at the browser level (If you want to do that, that's ok, but don't try to send it as part of your PR)

@qgatssdev qgatssdev requested a review from gndelia March 3, 2025 20:46
gabmontes
gabmontes previously approved these changes Mar 4, 2025
@gndelia
Copy link
Contributor

gndelia commented Mar 4, 2025

Conflicts!

@qgatssdev
Copy link
Contributor Author

qgatssdev commented Mar 4, 2025

Conflicts!

@gndelia I've resolved the conflicts locally and trying to push. Currently getting this lint error while trying to push
Screenshot 2025-03-04 at 9 26 33 PM

@qgatssdev
Copy link
Contributor Author

Conflicts!

@gndelia I've resolved the conflicts locally and trying to push. Currently getting this lint error while trying to push Screenshot 2025-03-04 at 9 26 33 PM

@gndelia

@gndelia
Copy link
Contributor

gndelia commented Mar 5, 2025

@qgatssdev for changes under subgraph/* folder, commit with --no-verify. We're trying to fix that..

@qgatssdev qgatssdev force-pushed the handle-error-queries-on-subgraphs branch 2 times, most recently from d14357c to b6240e6 Compare March 5, 2025 15:10
@qgatssdev qgatssdev force-pushed the handle-error-queries-on-subgraphs branch from b6240e6 to 6b526b6 Compare March 5, 2025 15:23
@qgatssdev
Copy link
Contributor Author

@gndelia Fixed! Sorry about the messy history, I ran into a problem 😅

@qgatssdev qgatssdev requested a review from gabmontes March 5, 2025 15:32
Comment on lines 121 to 124
(response: GraphResponse<{ _meta: { block: { number: number } } }>) => (
checkGraphQLErrors(response),
'data' in response && response.data._meta.block.number
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hard to read. Ok, perhaps this one exactly not, but the other .then for GetBtcWithdrawalsQueryResponse becomes very large. I think here, it could be better to add a regular function.

In addition to that, there's a bit of a redundant check for 'data' in response. We know that if checkGraphQLErrors did not throw, then response must be a successful type. I think we can improve that like this:

Update GraphResponse<T> to something like this:

type SuccessResponse<T> = { data: T }
type ErrorResponse = { errors: { message: string }[] }
type GraphResponse<T> = SuccessResponse<T> | ErrorResponse

Then, you can update checkGraphQLErrors to something like this

/**
 * Helper function to check for errors in GraphQL responses
 * @param response The GraphQL response to check
 * @throws Error if the response contains errors
 */
function checkGraphQLErrors<T>(
  response: GraphResponse<T>,
): asserts response is SuccessResponse<T> {
  // Check if response has errors
  if ('errors' in response && response.errors.length > 0) {
    // Extract error messages and join them
    const errorMessages = response.errors.map(e => e.message).join(', ')
    throw new Error(`GraphQL Error: ${errorMessages}`)
  }
}

You can check the docs for that asserts response is SuccessResponse<T>. It works very similar to a user-defined Type Guard.

If we do that, then we can finally replace the function that I am commenting with

Suggested change
(response: GraphResponse<{ _meta: { block: { number: number } } }>) => (
checkGraphQLErrors(response),
'data' in response && response.data._meta.block.number
),
.then(
(response: GraphResponse<{ _meta: { block: { number: number } } }>) => {
checkGraphQLErrors(response)
// At this point, the compiler knows that `response` is of type `SuccessResponse` and that `response.data` is valid. No need to check again!
return response.data._meta.block.number
},
)

Let's do that, and do something similar for the other returned functions here.

We're getting closer to an optimal solution!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gndelia Thanks for the review! I learnt something new, I've made the changes.

@qgatssdev qgatssdev requested a review from gndelia March 7, 2025 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle Error queries on subgraphs
3 participants